import 'package:azlistview/azlistview.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/module/match/list/lq_match_filter_logic.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/utils/constant.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/base/wqs_button.dart';
import 'package:ball_lion/widgets/loading/no_data_widget.dart';

import 'package:ball_lion/widgets/base/wqs_text.dart';

class LqFilterPage extends StatefulWidget {
  const LqFilterPage({super.key});

  @override
  State<LqFilterPage> createState() => _LqFilterPageState();
}

class _LqFilterPageState extends State<LqFilterPage>
    with TickerProviderStateMixin {
  late LqMatchFilterLogic filterLogic;
  String tag = Get.arguments;
  final List<String> title = [
    '全部',
    '热门',
    '竞篮',
  ];
  late TabController _controller;

  @override
  void initState() {
    super.initState();
    var matchType = LqMatchType.all;
    if (tag.contains(Constant.matchFilterTagHot)) {
      filterLogic = Get.find<LqMatchFilterLogic>(tag: tag);
      matchType = LqMatchType.hot;
    } else if (tag == Constant.matchFilterTagAll) {
      filterLogic = Get.find<LqMatchFilterLogic>(tag: tag);
      matchType = LqMatchType.all;
    } else if (tag == Constant.matchFilterTagBegin) {
      filterLogic = Get.find<LqMatchFilterLogic>(tag: tag);
      matchType = LqMatchType.begin;
    } else if (tag == Constant.matchFilterTagSchedule) {
      filterLogic = Get.find<LqMatchFilterLogic>(tag: tag);
      matchType = LqMatchType.schedule;
    } else if (tag == Constant.matchFilterTagResult) {
      filterLogic = Get.find<LqMatchFilterLogic>(tag: tag);
      matchType = LqMatchType.end;
    } else if (tag == Constant.matchFilterTagFocus) {
      filterLogic =
          Get.find<LqMatchFilterLogic>(tag: Constant.matchFilterTagFocus);
      matchType = LqMatchType.focus;
    }
    _controller = TabController(
        length: title.length,
        vsync: this,
        initialIndex: filterLogic.leagueType.index);
    // leagueController.leagueType = LeagueType.all;
    filterLogic.initLeague(matchType);
  }

  @override
  Widget build(BuildContext context) {
    // showList = [];
    // List<MatchTypeEntity> samePinyinList = [];
    // String tag = '';
    // matchList.forEach((element) {
    //   if (element.tagIndex == tag) {
    //     samePinyinList.add(element);
    //   } else {
    //     showList.add(samePinyinList);
    //     samePinyinList = [element];
    //     tag = element.tagIndex!;
    //   }
    // });
    return Scaffold(
        appBar: Styles.appBar(
          title: QsText('赛事筛选'),
        ),
        // backgroundColor: Colours.greyEDEDED,
        body: SafeArea(
          child: Column(
            children: [
              Container(
                color: Colours.white,
                height: 34,
                child: Styles.tabbar(
                  tabs: title.map((f) {
                    return QsText(f);
                  }).toList(),
                  onTap: (value) => tapTabbar(value),
                  // labelColor: Colours.white,
                  controller: _controller,
                  fontSize: 16,
                  fontWeight: FontWeight.w400,
                  unselectedFontWeight: FontWeight.w400,
                  labelColor: Colours.B6,
                  unselectedLabelColor: Colours.grey66,
                ),
              ),
              Divider(height: 0.5, color: Colours.greyEE),
              Expanded(
                  child: TabBarView(
                      controller: _controller,
                      children: List.generate(title.length, (index) {
                        if (index == 0) {
                          return const MatchFilterView();
                        } else {
                          return LqFilterView1(LqFilterType.values[index]);
                        }
                      }))),
              Divider(height: 0.5, color: Colours.greyEE),
              bottomBar()
            ],
          ),
        ));
  }

  void tapTabbar(index) {
    var leagueType = LqFilterType.all;
    if (index == 1) {
      leagueType = LqFilterType.rm;
    } else if (index == 2) {
      leagueType = LqFilterType.jc;
    }
    filterLogic.onSelectLeagueType(leagueType);
  }

  Widget bottomBar() {
    return GetBuilder<LqMatchFilterLogic>(
        tag: tag,
        builder: (controller) {
          return Container(
            // height: 50,
            color: Colours.white,
            padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            child: Row(children: [
              QsButton(
                  onPressed: () {
                    Utils.onEvent('lqsxy_sxan', params: {'lqsxy_sxan': '0'});
                    filterLogic.onSelectOneLevel();
                  },
                  text: '一级',
                  foregroundColor: Colours.grey66,
                  backgroundColor: Colours.greyF7,
                  textStyle:
                      TextStyle(fontWeight: FontWeight.w400, fontSize: 14),
                  borderRadius: 8,
                  width: 52,
                  height: 40),
              SizedBox(width: 8),
              QsButton(
                  onPressed: () {
                    Utils.onEvent('lqsxy_sxan', params: {'lqsxy_sxan': '1'});
                    filterLogic.onSelectAll();
                  },
                  text: '全选',
                  foregroundColor: Colours.grey66,
                  backgroundColor: Colours.greyF7,
                  textStyle:
                      TextStyle(fontWeight: FontWeight.w400, fontSize: 14),
                  borderRadius: 8,
                  width: 52,
                  height: 40),
              SizedBox(width: 8),
              QsButton(
                  onPressed: () {
                    Utils.onEvent('lqsxy_sxan', params: {'lqsxy_sxan': '2'});
                    filterLogic.onSelectReverse();
                  },
                  text: '反选',
                  foregroundColor: Colours.grey66,
                  backgroundColor: Colours.greyF7,
                  textStyle:
                      TextStyle(fontWeight: FontWeight.w400, fontSize: 14),
                  borderRadius: 8,
                  width: 52,
                  height: 40),
              SizedBox(width: 8),
              Text.rich(TextSpan(children: [
                TextSpan(
                    text: '隐藏',
                    style: TextStyle(fontSize: 11, color: Colours.grey66)),
                TextSpan(
                    text: '${filterLogic.hideMatchNum}',
                    style: TextStyle(fontSize: 11, color: Colours.B6)),
                TextSpan(
                    text: '场',
                    style: TextStyle(fontSize: 11, color: Colours.grey66))
              ])),
              Spacer(),
              QsButton(
                onPressed: () {
                  Utils.onEvent('sxy_qd');
                  filterLogic.onConfirm();
                },
                text: '确定',
                borderRadius: 8,
                height: 40,
                width: 92,
                foregroundColor: Colours.white,
                backgroundColor: Colours.B6,
                // side: BorderSide(width: 1),
              )
            ]),
          );
        });
  }
}

class MatchFilterView extends StatefulWidget {
  const MatchFilterView({super.key});

  @override
  State<MatchFilterView> createState() => _MatchFilterViewState();
}

class _MatchFilterViewState extends State<MatchFilterView> {
  late LqMatchFilterLogic leagueController;
  final logicTag = Get.arguments;
  @override
  void initState() {
    super.initState();
    leagueController = Get.find<LqMatchFilterLogic>(tag: logicTag);
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LqMatchFilterLogic>(
      tag: logicTag,
      builder: (_) {
        return Container(
          // color: Colours.greyEDEDED,
          child: leagueController.indexLeagueEntity.length == 0
              ? NoDataWidget()
              : AzListView(
                  data: leagueController.indexLeagueEntity,
                  itemCount: leagueController.indexLeagueEntity.length,
                  itemBuilder: ((context, index) {
                    return groupdLeague(index);
                  }),
                  physics: const BouncingScrollPhysics(),
                  indexBarData: SuspensionUtil.getTagIndexList(
                      leagueController.indexLeagueEntity),
                  // indexBarHeight: 500,
                  indexBarWidth: 26,
                  indexBarOptions: const IndexBarOptions(
                      needRebuild: true,
                      selectItemDecoration: BoxDecoration(
                          shape: BoxShape.circle, color: Colours.B6),
                      textStyle: TextStyle(color: Colours.N9, fontSize: 10),
                      selectTextStyle:
                          TextStyle(color: Colours.white, fontSize: 10)),
                ),
        );
      },
    );
  }

  Widget groupdLeague(index) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Offstage(
          offstage:
              leagueController.indexLeagueEntity[index].isShowSuspension !=
                  true,
          child: Container(
              padding: EdgeInsets.only(left: 16, top: 12),
              width: double.infinity,
              // color: Colours.greyEDEDED,
              child: QsText(
                index == 0 &&
                        (leagueController.indexLeagueEntity[index].tagIndex ==
                            '★')
                    ? '热门赛事'
                    : leagueController.indexLeagueEntity[index].tagIndex,
                fontWeight: FontWeight.w500,
              )),
        ),
        Container(
          // color: Colours.greyEDEDED,
          width: double.infinity,
          // decoration: BoxDecoration(color: Colors.red),
          padding: const EdgeInsets.fromLTRB(16, 8, 26, 0),
          child: Wrap(
              spacing: 12,
              runSpacing: 10,
              children: leagueController.indexLeagueEntity[index].leagues
                  .map((e) => GestureDetector(
                        onTap: () => leagueController.onTapLeague(e),
                        child: Container(
                          padding:
                              EdgeInsets.symmetric(vertical: 8, horizontal: 10),
                          decoration: BoxDecoration(
                              color: e.isSelected ? Colours.B1 : Colours.white,
                              borderRadius: BorderRadius.circular(8),
                              border: Border.all(
                                  color: e.isSelected
                                      ? Colours.B6
                                      : Colours.greyCC,
                                  width: 0.5)),
                          width: (Get.width - 67) / 3,
                          height: 34,
                          child: Row(children: [
                            Expanded(
                              child: QsText(
                                e.leagueName!,
                                fontSize: 13,
                                color: e.isSelected ? Colours.B6 : Colours.N9,
                              ),
                            ),
                            QsText('${e.matchNum}场',
                                fontSize: 11,
                                color:
                                    e.isSelected ? Colours.B6 : Colours.grey66)
                          ]),
                        ),
                      ))
                  .toList()),
        ),
      ],
    );
  }
}

class LqFilterView1 extends StatelessWidget {
  final LqFilterType type;
  const LqFilterView1(this.type, {super.key});

  @override
  Widget build(BuildContext context) {
    String tag = Get.arguments;
    return GetBuilder<LqMatchFilterLogic>(
      tag: tag,
      builder: (controller) {
        return controller.getLeague(type).length == 0
            ? NoDataWidget()
            : Container(
                // color: Colours.greyEDEDED,
                padding: EdgeInsets.all(20),
                child: SingleChildScrollView(
                  child: Wrap(
                      spacing: 20,
                      runSpacing: 10,
                      children: controller
                          .getLeague(type)
                          .map((e) => GestureDetector(
                                onTap: () => controller.onTapLeague(e),
                                child: Container(
                                  padding: EdgeInsets.symmetric(
                                      vertical: 8, horizontal: 10),
                                  decoration: BoxDecoration(
                                      color: e.isSelected
                                          ? Colours.B1
                                          : Colours.white,
                                      borderRadius: BorderRadius.circular(8),
                                      border: Border.all(
                                          color: e.isSelected
                                              ? Colours.B6
                                              : Colours.greyCC,
                                          width: 0.5)),
                                  width: (Get.width - 80) / 3 - 1,
                                  height: 34,
                                  child: Row(children: [
                                    Expanded(
                                      child: QsText(
                                        e.leagueName!,
                                        fontSize: 13,
                                        color: e.isSelected
                                            ? Colours.B6
                                            : Colours.N9,
                                      ),
                                    ),
                                    QsText('${e.matchNum}场',
                                        fontSize: 11,
                                        color: e.isSelected
                                            ? Colours.B6
                                            : Colours.grey66)
                                  ]),
                                ),
                              ))
                          .toList()),
                ),
              );
      },
    );
  }
}
